'use strict';
const router = require('koa-router')()
const middle = require('../middlewares')
const util = require('util')
const fdnService = require('../service/foundation')
const pointService = require('../service/point')

const {
    PointRule,
    PointRuleType,
    PointDetail,
    sequelize
    } = require('../models')

//sequelize.sync()

router.post('/rule', async function (ctx, next) {
    const data = ctx.pick('type_id', 'name', 'title', 'alteration', 'coefficient', 'start_time', 'end_time')

    //@todo check

    data.created_by = 0
    const rule = await PointRule.$create(data)

    ctx.send({rule})
})

/**
 * 获取所有积分规则
 */
router.get('/rule', middle.pagination, async function (ctx, next) {
    const {state, query} = ctx
    const { pagination } = state
    const rules = await PointRule.$pagination(pagination, {
        order: [
            ['id', 'ASC']
        ],
        raw: true
    })

    ctx.send({rules, pagination})
})


router.get('/rule/:id(\\d{1,20})', async function (ctx, next) {
    const id = ctx.params.id

    const rule = await PointRule.$findById(id)

    if (!rule) {
        return ctx.throw(404,'POINT_RULE_NOT_FOUND')
    }

    ctx.send({rule})
})

router.put('rule/:id(\\d{1,20})', async function (ctx, next) {
    const id = ctx.params.id
    const data = ctx.pick('type_id', 'name', 'title', 'alteration', 'start_time', 'end_time', 'is_valid')

    if (data.name && !data.name.trim().length) {
        return ctx.throw(400, 'POINT_RULE_NAME_REQUIRE')
    }

    data.updated_by = 0

    const rs = await PointRule.$update(id, data)

    if (!rs) {
        return ctx.throw(404, 'POINT_RULE_NOT_FOUND')
    }

    ctx.ok()
})

router.del('rule/:id(\\d{1,20})', async function (ctx, next) {
    const id = ctx.params.id

    //@todo check

    const rs = await PointRule.$destroy(id)

    if (!rs) {
        return ctx.throw(404, 'POINT_RULE_NOT_FOUND')
    }

    ctx.ok()
})

router.get('/user/:userId(\\d{1,20}/detail)',middle.pagination, async function (ctx, next) {
    const {state, params} = ctx
    const { userId } = params
    const { pagination } = state
    const details = await PointDetail.$pagination(pagination, {
        where: {
          $or: [{
              user_id: userId,
          },{
              relation_user_id: userId,
          }
          ]
        },
        order: [
            ['id', 'ASC']
        ],
        raw: true
    })
    ctx.send({details})
})



router.post('/', async function (ctx, next) {
    //todo deal point depend snapshot
    const {module_itemId, createdAt} = ctx.request.body
    const rlt = await fdnService.snapshotQuery(module_itemId,createdAt)
    const snapshots = rlt.snapshots
    if (snapshots.Items.length === 0) {
        return ctx.throw(404, 'INTEGRAL_SNAPSHOT_NOT_FOUND')
    }
    if (snapshots.Items[snapshots.Items.length - 1].createdAt !== createdAt) {
        return ctx.throw(404, 'INTEGRAL_SNAPSHOT_NOT_FOUND')
    }
    const ruleList = await pointService.checkSnapshot(snapshots.Items)

    const pointdetails = await pointService.pointProcess(ruleList)

    ctx.send({pointdetails})
})



module.exports = router